package com.kinghunt.misp.web.controller.materialx;

import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.dto.page.PageInfo;
import com.kinghunt.misp.common.dto.materialx.MaterialDTO;
import com.kinghunt.misp.common.dto.materialx.MaterialStockDTO;
import com.kinghunt.misp.common.dto.userx.UserDTO;
import com.kinghunt.misp.common.enums.materialx.MaterialTypeEnum;
import com.kinghunt.misp.common.param.materialx.MaterialParam;
import com.kinghunt.misp.common.param.materialx.MaterialStockParam;
import com.kinghunt.misp.service.api.materialx.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

import static com.kinghunt.misp.web.context.SystemContext.getCurrentUser;

/**
 * 耗材管理
 *
 * @Author ciaran
 * @Date 2023/7/13 21:58l
 */
@RestController
@RequestMapping("/api/material/")
public class MaterialController {

    @Autowired(required = false)
    private MaterialService materialService;

    /**
     * 列表显示：显示耗材列表
     */
    @GetMapping("getMaterialList")
    public ApiResult<PageInfo<MaterialDTO>> getMaterialList(MaterialParam materialParam) {
        return materialService.getMaterialList(materialParam);
    }
    /**
     * 列表显示：显示耗材出入库列表
     */
    @GetMapping("getMaterialStockList")
    public ApiResult<PageInfo<MaterialStockDTO>> getMaterialStockList(Long materialId, int pageNum, int pageSize) {
        if(Objects.isNull(materialId)){
            return ApiResult.failOfMessage("查询失败，请输入耗材信息！");
        }
        MaterialParam materialParam = new MaterialParam();
        materialParam.setMaterialId(materialId);
        materialParam.setPageNum(pageNum);
        materialParam.setPageSize(pageSize);
        return materialService.getMaterialStockList(materialParam);
    }
    /**
     * 耗材详情展示
     */
    @GetMapping("getMaterialDetail")
    public ApiResult<MaterialDTO> getMaterialDetail(Long materialId) {
        if(Objects.isNull(materialId)){
            return ApiResult.failOfMessage("查询失败，请输入客户信息！");
        }
        return materialService.getMaterialDetail(materialId);
    }
    /**
     * 新增耗材
     */
    @PostMapping(value = "addMaterial", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult addMaterial(@Valid @RequestBody MaterialParam materialParam) {
        if(Objects.isNull(materialParam.getMaterialNo())){
            return ApiResult.failOfMessage("新增失败，请输入耗材编号！");
        }
        UserDTO loginUser = getCurrentUser();
        materialParam.setCreatorId(loginUser.getId());
        materialParam.setUpdaterId(loginUser.getId());
        return materialService.addMaterial(materialParam);
    }
    /**
     * 修改耗材
     */
    @PostMapping(value = "updateMaterial", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult updateMaterial(@Valid @RequestBody MaterialParam materialParam) {
        UserDTO loginUser = getCurrentUser();
        materialParam.setUpdaterId(loginUser.getId());
        return materialService.updateMaterial(materialParam);
    }
    /**
     * 删除耗材
     *
     * @param materialId 耗材ID
     * @return 是否成功
     */
    @DeleteMapping("deleteMaterial")
    public ApiResult<Boolean> deleteMaterial(Long materialId) {
        if(Objects.isNull(materialId)){
            return ApiResult.failOfMessage("删除失败，请输入耗材信息！");
        }
        return materialService.deleteMaterial(materialId);
    }

    /**
     * 耗材入库
     */
    @PostMapping(value = "inMaterial", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult inMaterialStock(@Valid @RequestBody MaterialStockParam materialStockParam) {
        UserDTO loginUser = getCurrentUser();
        materialStockParam.setStockUserId(loginUser.getId());
        materialStockParam.setStockType(MaterialTypeEnum.MATERIAL_TYPE_2.getValue());
        return materialService.updateMaterialStock(materialStockParam);
    }
    /**
     * 耗材出库
     */
    @PostMapping(value = "outMaterial", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResult outMaterialStock(@Valid @RequestBody MaterialStockParam materialStockParam) {
        UserDTO loginUser = getCurrentUser();
        materialStockParam.setStockUserId(loginUser.getId());
        materialStockParam.setStockType(MaterialTypeEnum.MATERIAL_TYPE_1.getValue());
        return materialService.updateMaterialStock(materialStockParam);
    }

}
